using System;
using System.Linq;
using AutoMapper;
using ExamServer.Entities;
using ExamServer.Models;

namespace ExamServer.ProFile
{
    public class ExamServerProfile:Profile
    {
        public ExamServerProfile()
        {
            CreateMap<Users,UserModel>()
            .ForMember(dest=>dest.Age,
                       config=>config.MapFrom(
                           src=>DateTime.Now.Year-DateTime.Parse(src.Birthday).Year
                           )
                    );
            CreateMap<QuestionType,QuestionTypeModel>()
            .ForMember(dest=>dest.StateDisplay,
                        config=>config.MapFrom(
                            src=>src.TypeState.ToString()
                        ));
            CreateMap<Menus,MenusModel>();
            CreateMap<Users,UserEditModel>();
            CreateMap<tbRolesAndMenus,tbUsersAndMenusModel>();
            CreateMap<viewUserHaveMenus,MenusModel>();
            CreateMap<Menus,TreeViewModel>()
            .ForMember(
                dest=>dest.id,
                config=>config.MapFrom(
                    src=>src.Id
                )
            )
            .ForMember(
                    dest=>dest.text,
                    config=>config.MapFrom(
                        src=>src.name
                    )
                );
            CreateMap<tbRoles,RoleOptModel>()
            .ForMember(dest=>dest.StateDisplay,
            config=>config.MapFrom(
                src=>src.RoleState.ToString()
            ))
            .ForMember(dest=>dest.mtree,
            config=>config.MapFrom(
                src=>string.Join(",",src.RolesAndMenus.Select(x=>x.MenuID).ToArray())
            ));
        }
    }
}
